<?php
/**
 * 个人资料
 * 
 */
 
include_once(GZ_PATH . '/includes/personal_func.php');
class personalAction extends Action{
    /**我的-个人资料
     */
    public function personal_info(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $user = get_userInfo($uid);
        GZ_Api::outPut($user);
    }
//修改个人资料
    public function edit_userinfo(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $data["headimg"] = _POST("headimg","");
        $data["nickname"] = _POST("nickname","");
        $data["sex"] = _POST("sex","");
        $data["height"] = _POST("height","");
        $data["weight"] = _POST("weight",""); 
        $data["blood_type"] = _POST("blood_type","");
        $data["feature_desc"] = _POST("feature_desc","");
        $data["adverse_effect"] = _POST("adverse_effect","");
        $sql = "SELECT `headimg` FROM ".$this->table("users")." WHERE `user_id`=".$uid;
        $old_headimg = $this->getOne($sql);
        $res = $this->autoExecute($this->table('users'), $data, 'UPDATE', "user_id = ".$uid);
        if($res){
            if($old_headimg != $data["headimg"]){
                @unlink($_SERVER['DOCUMENT_ROOT'].$old_headimg);
            }
            GZ_API::message("编辑成功",1);
        }else{
            GZ_API::message("编辑失败",2);
        }
    }

    //我的求助
    public function seek_help(){
       GZ_Api::authToken();
       $uid = _POST("user_id");
       $page_size   = GZ_Api::$pagination['count'];    //分页设置
       $page        = GZ_Api::$pagination['page'];     //分页设置
       $res=get_help($uid,$page,$page_size);
       if($res){
          GZ_Api::outPut($res);
        }else{
          GZ_Api::outPut(13);
      }
       
    }
    //我的救助
    public function my_rescue(){
       GZ_Api::authToken();
       $uid = _POST("user_id");
       $page_size   = GZ_Api::$pagination['count'];    //分页设置
       $page        = GZ_Api::$pagination['page'];     //分页设置
       $res=get_rescue($uid,$page,$page_size);
       if($res){
          GZ_Api::outPut($res);
        }else{
          GZ_Api::outPut(13);
      }
    }
    //我的钱包
     public function my_wallet(){
       GZ_Api::authToken();
       $uid = _POST("user_id");
       $data=wallet_account($uid);
       GZ_Api::outPut($data);
     }
    //收支明细
    public function account_log(){
       GZ_Api::authToken();
       $uid = _POST("user_id");
       $type=1;//金额
       $page_size   = GZ_Api::$pagination['count'];    //分页设置
       $page        = GZ_Api::$pagination['page'];     //分页设置  
       $res = get_account($uid,$type,$page,$page_size);
	   if($res){
	        GZ_Api::outPut($res);
	      }else{
	        GZ_Api::outPut(13);
	    }
       
     } 
   //我的积分
    public function integral_log(){
       GZ_Api::authToken();
       $uid = _POST("user_id");
       $type=2;//积分
       $page_size   = GZ_Api::$pagination['count'];    //分页设置
       $page        = GZ_Api::$pagination['page'];     //分页设置  
       $res = get_account($uid,$type,$page,$page_size);
	   if($res){
	        GZ_Api::outPut($res);
	      }else{
	        GZ_Api::outPut(13);
	    }
       
     } 
  //我的套餐
   public function package(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $data=array();
        $data['mycard']=get_mycard($uid);//我的套餐
        $data['card_list']=get_card();//会员套餐列表
        GZ_Api::outPut($data);
   }
   
   public function card_info(){
     $card_id=_POST('card_id',0);
     $card_info = get_cardInfo($card_id);
     GZ_Api::outPut($card_info);
   }

   //套餐详情
   public function card_detail(){
     $card_id=_POST('card_id',0);
     $data['card_info_h5']='http://www.fas.com/Mapi/public/html/card_detail.php?card_id='.$card_id;
     GZ_Api::outPut($card_info);
   }

   //会员认证
   public function user_auth(){
     GZ_Api::authToken();
     $data=array();
     $type=_POST('type','');
     $data['user_id'] = _POST("user_id",0);
     $data['auth_name']=_POST('user_name','');//真实姓名
     $data['identity_card']=_POST('identity_card','');//身份证号
     if(empty($data['auth_name'])){GZ_API::message("真实姓名不能为空",2);}
     if(empty($data['identity_card'])){GZ_API::message("身份证号码不能为空",2);}
     $data['face_card']=_POST('img_url1','');//身份证正面
     $data['back_card']=_POST('img_url2','');//身份证反面
     $data['auth_type']=1;
     $data['auth_status']=0;
     $data['add_time']=time();
     $auth_info=$this->getRow('select * from '.$this->table('user_auth').' where user_id='.$data['user_id']);
     if($auth_info['auth_status'] == 0 ){     
       GZ_API::message("该申请已提交过,请等待验证",2);
     }else if ($auth_info['auth_status'] == 1 ) {
        GZ_API::message("该申请已通过！！",2);
     }
     if($type=='add'){
       $res = $GLOBALS["db"]->autoExecute($GLOBALS["db"]->table("user_auth"),$data,"INSERT");
     }else{
       $res = $this->autoExecute($this->table('user_auth'), $data, 'UPDATE', "user_id = ".$data['user_id']);
     }
     
     if($res){
          GZ_Api::message("提交成功",1);
      }else{
          GZ_Api::message("提交失败",2);
      }
   }
  //医师认证
  public function doctor_auth(){
     GZ_Api::authToken();
     $data=array();
     $type=_POST('type','');
     $data['user_id'] = _POST("user_id",0);
     $data['auth_name']=_POST('user_name','');//真实姓名
     $data['identity_card']=_POST('identity_card','');//身份证号
     $data['auth_project']=_POST('project','');//主治项目
     $data['auth_hospital']=_POST('hospital','');//所在医院
     if(empty($data['auth_name'])){GZ_API::message("真实姓名不能为空",2);}
     if(empty($data['identity_card'])){GZ_API::message("身份证号码不能为空",2);}
     if(empty($data['auth_project'])){GZ_API::message("真实姓名不能为空",2);}
     if(empty($data['auth_hospital'])){GZ_API::message("身份证号码不能为空",2);}
     $data['face_card']=_POST('img_url1','');//身份证正面
     $data['back_card']=_POST('img_url2','');//身份证反面
     $data['auth_img']=_POST('img_url3','');//医师执照
     $data['auth_type']=2;
     $data['auth_status']=0;
     $data['add_time']=time();
     $auth_info=$this->getRow('select * from '.$this->table('user_auth').' where user_id='.$data['user_id']);
     if($auth_info['auth_status'] == 0 ){
        GZ_API::message("该申请已提交过,请等待验证",2);
     }else if ($auth_info['auth_status'] == 1 ) {
        GZ_API::message("该申请已通过！！",2);
     }
     if($type=='add'){
        $res = $GLOBALS["db"]->autoExecute($GLOBALS["db"]->table("user_auth"),$data,"INSERT");
     }else{
       $res = $this->autoExecute($this->table('user_auth'), $data, 'UPDATE', "user_id = ".$data['user_id']);
     }
     
    
     if($res){
          GZ_Api::message("提交成功",1);
      }else{
          GZ_Api::message("提交失败",2);
      }
   }

//获取验证状态
   public function get_auth(){
      GZ_Api::authToken();
      $user_id = _POST("user_id",0);
      $type=_POST('type','');//1：普通用户 2：医师
      $auth_info=$this->getRow('select * from '.$this->table('user_auth').' where user_id='.$user_id);
      $data=array();
      if($auth_info!=null){
         if($type==1){
           if($auth_info['auth_status']==0){
              $data['status']=1;
              $data['status_name']='你的实名认证审核中';
           }else if($auth_info['auth_status']==1){
              $data['status']=2;
              $data['status_name']='认证审核通过';
           }else if($auth_info['auth_status']==2){
              $data['status']=3;
              $data['status_name']='不通过';
           } 
           $data['auth_name']=$auth_info['auth_name'];
           $data['identity_card']=$auth_info['identity_card'];
           $data['remark']=isset($auth_info['remark'])?$auth_info['remark']:'';
         }else{
           if($auth_info['auth_status']==0){
              $data['status']=1;
              $data['status_name']='你的实名认证审核中';
           }else if($auth_info['auth_status']==1){
              $data['status']=2;
              $data['status_name']='认证审核通过';
           }else if($auth_info['auth_status']==2){
              $data['status']=3;
              $data['status_name']='不通过';
           }   
           $data['auth_name']=$auth_info['auth_name'];
           $data['identity_card']=$auth_info['identity_card'];
           $data['remark']=isset($auth_info['remark'])?$auth_info['remark']:'';
         }


      }else{
        $data['status']=0;//未提交状态
        $data['status_name']='未提交';
        $data['auth_name']='';
        $data['identity_card']='';
        $data['remark']='';
      }
     
      GZ_Api::outPut($data);
   }

//重新验证
   public function update_auth(){
      GZ_Api::authToken();
      $user_id = _POST("user_id",0);
      $type=_POST('type','');//1：普通用户 2：医师
      $data=array();
      $info=$this->getRow('select * from '.$this->table('user_auth').' where user_id='.$user_id.' and auth_status=2');
      if($info!=null){
        $data['user_id']=$info['user_id'];
        $data['auth_name']=$info['auth_name'];
        $data['identity_card']=$info['identity_card'];//身份证号
        $data['auth_project']=$info['auth_project'];
        $data['auth_hospital']=$info['auth_hospital'];
        $data['face_card']=$info['face_card'];
        $data['back_card']=$info['back_card'];
        $data['auth_img']=$info['auth_img'];
      }
      GZ_Api::outPut($data);

   }
   //标签
   public function label(){
     GZ_Api::authToken();
     $user_id =_POST("user_id");

     $label1=str_len(_POST('label1',''));
     $label2=str_len(_POST('label2',''));
     $label3=str_len(_POST('label3',''));
     $label4=str_len(_POST('label4',''));
     if($label1>12 || $label2>12 || $label3>12 || $label4>12){
       GZ_API::message("标签不能超过6个字",2); 
     }
     $data=array(
        _POST('label1',''),
        _POST('label2',''),
        _POST('label3',''),
        _POST('label4',''),
      );
     
     $info['label']=json_encode($data,JSON_UNESCAPED_UNICODE);
     $res = $this->autoExecute($this->table('users'), $info, 'UPDATE', "user_id = ".$user_id);
     if($res){
          GZ_Api::message("编辑成功",1);
      }else{
          GZ_Api::message("编辑失败",2);
      }

   }
   // 
   //标签首页
   public function label_list(){
     GZ_Api::authToken();
     $user_id =_POST("user_id");
     $sql='select label from '.$this->table('users').' where user_id='.$user_id;
     $res=$GLOBALS['db']->getRow($sql);
     $label=json_decode($res['label'],true);

     $data=array();
     if($label!=null){
        foreach ($label as $key => $value) {  
         $row['label_index']= $key;
         $row['label_name']= $value;
         $data[] = $row;
        }
     }
     

     GZ_Api::outPut($data);
   }
   //添加修改标签
   public function edit_label(){
     GZ_Api::authToken();
     $user_id=_POST('user_id','');
     $type=_POST('type','');//add:添加 edit：修改
     $label_len=str_len(_POST('label',''));//标签
     if($label_len>12){ 
       GZ_API::message("标签不能超过6个字",2); 
     }
     $label=_POST('label','');
     $user=$this->getOne('select label from '.$this->table('users').' where user_id='.$user_id); 
     //添加标签
     if($type=='add'){      
       if($user!=null){
          $old_label=json_decode($user,true);
          if(count($old_label)>=4){
            GZ_API::message("不能超过4个标签",2);
          }
          array_push($old_label,$label);
       }else{
          $old_label=array(
            $label,
            );
       }  
       $data['label']=json_encode($old_label,JSON_UNESCAPED_UNICODE);
     }else{
      //修改标签
         $label_index=_POST('label_index',0);//指定的下标
         $old_label=json_decode($user,true);
         foreach ($old_label as $k => $v) {
               $old_label[$label_index]=$label;
           } 
         $data['label']=json_encode($old_label,JSON_UNESCAPED_UNICODE);     
     }
  
     $res = $this->autoExecute($this->table('users'), $data, 'UPDATE', "user_id = ".$user_id);
     if($res){
          GZ_Api::message("编辑成功",1);
      }else{
          GZ_Api::message("编辑失败",2);
      }
   }

}

?>